<?php
namespace System\Controller;

use Common\Controller\AdminbaseController;

class UsersController extends AdminbaseController{

	protected $users_model;

	public function _initialize() {
		parent::_initialize();
		$this->users_model = D("SystemUsers");
	}

	// 用户列表
	public function index(){
		/**搜索条件**/
		$class = I("request.trem");

		if(!empty($class)){
			$where['user_type'] = array('eq',$class);
		}
		$start_time=I('request.start_time');
		if(!empty($start_time)){
			$where['create_time']=array(
				array('EGT',strtotime($start_time))
			);
		}
		$end_time=I('request.end_time');
		if(!empty($end_time)){
			if(empty($where['create_time'])){
				$where['create_time']=array();
			}
			array_push($where['create_time'], array('ELT',strtotime($end_time)));
		}
		$user_login = I('request.keyword');
		if($user_login){
			$where['mobile'] = array('like',"%$user_login%");
		}
		$count=$this->users_model->where($where)->count();
		$page = $this->page($count, 20);
		$users = $this->users_model
			->where($where)
			->where('user_type != 2')
			->order("create_time DESC")
			->limit($page->firstRow, $page->listRows)
			->select();
		foreach ($users as $key => $value){
			if ($users[$key]['avatar']){
				$users[$key]['avatar'] = $_SERVER['SERVER_NAME'].'/data/upload/avatar/'.$users[$key]['avatar'];
			}
		}

		//用户角色分类
		$classification_list = array(
			'1' => array(
				'class_id' => '1',
				'class_name' => '用户',
			),
			'2' => array(
				'class_id' => '3',
				'class_name' => 'VIP会员',
			),
			'3' => array(
				'class_id' => '4',
				'class_name' => '超级VIP会员',
			),
		);

		$this->assign("formget",array_merge($_GET,$_POST));
		$this->assign('class',$classification_list);
		$this->assign("page", $page->show('Admin'));
		$this->assign("users",$users);
		$this->display();
	}

	// 管理员删除
	public function delete(){
		$id = I('get.id',0,'intval');
		if($id==1){
			$this->error("最高管理员不能删除！");
		}

		if ($this->users_model->delete($id)!==false) {
			M("RoleUser")->where(array("user_id"=>$id))->delete();
			$this->success("删除成功！");
		} else {
			$this->error("删除失败！");
		}
	}

	// 停用管理员
	public function ban(){
		$id = I('get.id',0,'intval');
		if (!empty($id)) {
			$result = $this->users_model->where(array("id"=>$id,"user_type"=>1))->setField('user_status','0');
			if ($result!==false) {
				$this->success("用户停用成功！", U("Users/index"));
			} else {
				$this->error('用户停用失败！');
			}
		} else {
			$this->error('数据传入失败！');
		}
	}

	// 启用管理员
	public function cancelban(){
		$id = I('get.id',0,'intval');
		if (!empty($id)) {
			$result = $this->users_model->where(array("id"=>$id,"user_type"=>1))->setField('user_status','1');
			if ($result!==false) {
				$this->success("用户启用成功！", U("Users/index"));
			} else {
				$this->error('用户启用失败！');
			}
		} else {
			$this->error('数据传入失败！');
		}
	}


// 用户头像api
	public function avatar(){
		$users_model=D("SystemUsers");
		$id=I("get.id",0,"intval");
		$find_user=$users_model->field('avatar')->where(array("id"=>$id))->find();

		$avatar=$find_user['avatar'];
		$avatar=preg_replace("/^avatar\//", '', $avatar);//2.2以后头像路径统一以avatar/开头
		$should_show_default=false;

		if(empty($avatar)){
			$should_show_default=true;
		}else{
			if(strpos($avatar,"http")===0){
				header("Location: $avatar");exit();
			}else{
				$avatar_dir=C("UPLOADPATH")."avatar/";
				$avatar=$avatar_dir.$avatar;
				if(file_exists($avatar)){
					$imageInfo = getimagesize($avatar);
					if ($imageInfo !== false) {
						$fp=fopen($avatar,"r");
						$file_size=filesize($avatar);
						$mime=$imageInfo['mime'];
						header("Content-type: $mime");
						header("Accept-Length:".$file_size);
						$buffer=1024*64;
						$file_count=0;
						//向浏览器返回数据
						while(!feof($fp) && $file_count<$file_size){
							$file_content=fread($fp,$buffer);
							$file_count+=$buffer;
							echo $file_content;
							flush();
							ob_flush();
						}
						fclose($fp);
					}else{
						$should_show_default=true;
					}
				}else{
					$should_show_default=true;
				}
			}


		}

		if($should_show_default){
			$imageInfo = getimagesize("public/images/headicon.png");
			if ($imageInfo !== false) {
				$mime=$imageInfo['mime'];
				header("Content-type: $mime");
				echo file_get_contents("public/images/headicon.png");
			}

		}
		exit();

	}





}